//
//  MBImageGalleryViewController.m
//  MobellCloud_ios
//
//  Created by cherry pu on 2018/1/22.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBImageGalleryViewController.h"

@interface MBImageGalleryViewController ()<MBImageGalleryViewDataSource>

@end

@implementation MBImageGalleryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureRecognizer)]];
    
    [self setupSelectedImageView];
    
    [self setupImageGallery];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

/**
 *  初始化占位图
 */
- (void)setupSelectedImageView
{
    UIImageView * selectedImageView = [[UIImageView alloc] init];
    selectedImageView.contentMode = UIViewContentModeScaleAspectFit;
    
    NSString* imageUrl;
    if ([self.allMessageArray[self.selectIndex] isKindOfClass:[NSString class]]) {
        imageUrl = self.allMessageArray[self.selectIndex];
    }
    
    if (![imageUrl containsString:@"http"]) {
        imageUrl = [NSString stringWithFormat:@"%@%@",BASE_IMAGE_URL,self.allMessageArray[self.selectIndex]];
    }
    
    UIImage* image = [[MBCacheService sharedInstance]fetchBannerImageWithUrl:self.allMessageArray[self.selectIndex]];
    if (image) {
        selectedImageView.image = image;
    }else {
        [[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderLowPriority progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            if (image) {
                selectedImageView.image = image;
                [[MBCacheService sharedInstance]cacheBannerImageWithUrl:self.allMessageArray[self.selectIndex] andImage:image];
                 }
        }];
    }
    
    [self.view addSubview:selectedImageView];
    self.selectedImageView = selectedImageView;
    
    [selectedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsZero);
    }];
}


/**
 *  添加画廊
 */
- (void)setupImageGallery
{
    MBImageGalleryView * imageGallery = [[MBImageGalleryView alloc] initWithEdgeOffset:[UIView lf_sizeFromIphone6:10]];
    imageGallery.imageItemArray = [self.allMessageArray copy];
    [self hideSelectedImageView];
    imageGallery.dataSource = self;
    [imageGallery setImagePositionIndex:self.selectIndex];;
    [self.view addSubview:imageGallery];
    self.imageGallery = imageGallery;
    
    [imageGallery mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsZero);
    }];
}


//隐藏展位图
- (void)hideSelectedImageView
{
    self.selectedImageView.hidden = YES;
}


#pragma mark - EvaImgaeGalleryDataSource
- (NSArray *)sourceImageItemArrayForGallery
{
    return self.allMessageArray;
}

/**
 *  处理单击手势
 */
- (void)handleTapGestureRecognizer
{
    [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION_GOODS_DETAIL_SCROLL_COMMENT object:nil userInfo:@{@"isBanner":@(self.isBanner)}];
    [self.navigationController popViewControllerAnimated:YES];
}


@end
